package cn.sh.yhk.mapper;

import cn.sh.yhk.model.InviteBids;
import cn.sh.yhk.model.InviteBidsExample;
import cn.sh.yhk.model.InviteBidsExample.Criteria;
import cn.sh.yhk.model.InviteBidsExample.Criterion;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public class InviteBidsSqlProvider {

    public String countByExample(InviteBidsExample example) {
        SQL sql = new SQL();
        sql.SELECT("count(*)").FROM("invite_bids");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String deleteByExample(InviteBidsExample example) {
        SQL sql = new SQL();
        sql.DELETE_FROM("invite_bids");
        applyWhere(sql, example, false);
        return sql.toString();
    }

    public String insertSelective(InviteBids record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("invite_bids");
        
        if (record.getId() != null) {
            sql.VALUES("id", "#{id,jdbcType=INTEGER}");
        }
        
        if (record.getNumberId() != null) {
            sql.VALUES("number_id", "#{numberId,jdbcType=VARCHAR}");
        }
        
        if (record.getCarModel() != null) {
            sql.VALUES("car_model", "#{carModel,jdbcType=VARCHAR}");
        }
        
        if (record.getEndTime() != null) {
            sql.VALUES("end_time", "#{endTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getItemName() != null) {
            sql.VALUES("item_name", "#{itemName,jdbcType=VARCHAR}");
        }
        
        if (record.getInviteId() != null) {
            sql.VALUES("invite_id", "#{inviteId,jdbcType=VARCHAR}");
        }
        
        if (record.getCarModelNumber() != null) {
            sql.VALUES("car_model_number", "#{carModelNumber,jdbcType=VARCHAR}");
        }
        
        if (record.getCarExplain() != null) {
            sql.VALUES("car_explain", "#{carExplain,jdbcType=VARCHAR}");
        }
        
        if (record.getReggion() != null) {
            sql.VALUES("reggion", "#{reggion,jdbcType=VARCHAR}");
        }
        
        if (record.getPrivince() != null) {
            sql.VALUES("privince", "#{privince,jdbcType=VARCHAR}");
        }
        
        if (record.getCity() != null) {
            sql.VALUES("city", "#{city,jdbcType=VARCHAR}");
        }
        
        if (record.getBugget() != null) {
            sql.VALUES("bugget", "#{bugget,jdbcType=VARCHAR}");
        }
        
        if (record.getType() != null) {
            sql.VALUES("type", "#{type,jdbcType=VARCHAR}");
        }
        
        if (record.getInviteBidsBrief() != null) {
            sql.VALUES("invite_bids_brief", "#{inviteBidsBrief,jdbcType=VARCHAR}");
        }
        
        if (record.getUrl() != null) {
            sql.VALUES("url", "#{url,jdbcType=VARCHAR}");
        }
        
        if (record.getOther() != null) {
            sql.VALUES("other", "#{other,jdbcType=VARCHAR}");
        }
        
        if (record.getReleaseTime() != null) {
            sql.VALUES("release_time", "#{releaseTime,jdbcType=VARCHAR}");
        }
        
        if (record.getRemarks() != null) {
            sql.VALUES("remarks", "#{remarks,jdbcType=VARCHAR}");
        }
        
        if (record.getFirm() != null) {
            sql.VALUES("firm", "#{firm,jdbcType=VARCHAR}");
        }
        
        if (record.getLatitude() != null) {
            sql.VALUES("latitude", "#{latitude,jdbcType=VARCHAR}");
        }
        
        if (record.getLongitude() != null) {
            sql.VALUES("longitude", "#{longitude,jdbcType=VARCHAR}");
        }
        
        if (record.getState() != null) {
            sql.VALUES("state", "#{state,jdbcType=INTEGER}");
        }
        
        if (record.getCreateTime() != null) {
            sql.VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getModifyTime() != null) {
            sql.VALUES("modify_time", "#{modifyTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getInviteBidsDetail() != null) {
            sql.VALUES("invite_bids_detail", "#{inviteBidsDetail,jdbcType=LONGVARCHAR}");
        }
        
        return sql.toString();
    }

    public String selectByExampleWithBLOBs(InviteBidsExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("id");
        } else {
            sql.SELECT("id");
        }
        sql.SELECT("number_id");
        sql.SELECT("car_model");
        sql.SELECT("end_time");
        sql.SELECT("item_name");
        sql.SELECT("invite_id");
        sql.SELECT("car_model_number");
        sql.SELECT("car_explain");
        sql.SELECT("reggion");
        sql.SELECT("privince");
        sql.SELECT("city");
        sql.SELECT("bugget");
        sql.SELECT("type");
        sql.SELECT("invite_bids_brief");
        sql.SELECT("url");
        sql.SELECT("other");
        sql.SELECT("release_time");
        sql.SELECT("remarks");
        sql.SELECT("firm");
        sql.SELECT("latitude");
        sql.SELECT("longitude");
        sql.SELECT("state");
        sql.SELECT("create_time");
        sql.SELECT("modify_time");
        sql.SELECT("invite_bids_detail");
        sql.FROM("invite_bids");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    public String selectByExample(InviteBidsExample example) {
        SQL sql = new SQL();
        if (example != null && example.isDistinct()) {
            sql.SELECT_DISTINCT("id");
        } else {
            sql.SELECT("id");
        }
        sql.SELECT("number_id");
        sql.SELECT("car_model");
        sql.SELECT("end_time");
        sql.SELECT("item_name");
        sql.SELECT("invite_id");
        sql.SELECT("car_model_number");
        sql.SELECT("car_explain");
        sql.SELECT("reggion");
        sql.SELECT("privince");
        sql.SELECT("city");
        sql.SELECT("bugget");
        sql.SELECT("type");
        sql.SELECT("invite_bids_brief");
        sql.SELECT("url");
        sql.SELECT("other");
        sql.SELECT("release_time");
        sql.SELECT("remarks");
        sql.SELECT("firm");
        sql.SELECT("latitude");
        sql.SELECT("longitude");
        sql.SELECT("state");
        sql.SELECT("create_time");
        sql.SELECT("modify_time");
        sql.FROM("invite_bids");
        applyWhere(sql, example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            sql.ORDER_BY(example.getOrderByClause());
        }
        
        return sql.toString();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        InviteBids record = (InviteBids) parameter.get("record");
        InviteBidsExample example = (InviteBidsExample) parameter.get("example");
        
        SQL sql = new SQL();
        sql.UPDATE("invite_bids");
        
        if (record.getId() != null) {
            sql.SET("id = #{record.id,jdbcType=INTEGER}");
        }
        
        if (record.getNumberId() != null) {
            sql.SET("number_id = #{record.numberId,jdbcType=VARCHAR}");
        }
        
        if (record.getCarModel() != null) {
            sql.SET("car_model = #{record.carModel,jdbcType=VARCHAR}");
        }
        
        if (record.getEndTime() != null) {
            sql.SET("end_time = #{record.endTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getItemName() != null) {
            sql.SET("item_name = #{record.itemName,jdbcType=VARCHAR}");
        }
        
        if (record.getInviteId() != null) {
            sql.SET("invite_id = #{record.inviteId,jdbcType=VARCHAR}");
        }
        
        if (record.getCarModelNumber() != null) {
            sql.SET("car_model_number = #{record.carModelNumber,jdbcType=VARCHAR}");
        }
        
        if (record.getCarExplain() != null) {
            sql.SET("car_explain = #{record.carExplain,jdbcType=VARCHAR}");
        }
        
        if (record.getReggion() != null) {
            sql.SET("reggion = #{record.reggion,jdbcType=VARCHAR}");
        }
        
        if (record.getPrivince() != null) {
            sql.SET("privince = #{record.privince,jdbcType=VARCHAR}");
        }
        
        if (record.getCity() != null) {
            sql.SET("city = #{record.city,jdbcType=VARCHAR}");
        }
        
        if (record.getBugget() != null) {
            sql.SET("bugget = #{record.bugget,jdbcType=VARCHAR}");
        }
        
        if (record.getType() != null) {
            sql.SET("type = #{record.type,jdbcType=VARCHAR}");
        }
        
        if (record.getInviteBidsBrief() != null) {
            sql.SET("invite_bids_brief = #{record.inviteBidsBrief,jdbcType=VARCHAR}");
        }
        
        if (record.getUrl() != null) {
            sql.SET("url = #{record.url,jdbcType=VARCHAR}");
        }
        
        if (record.getOther() != null) {
            sql.SET("other = #{record.other,jdbcType=VARCHAR}");
        }
        
        if (record.getReleaseTime() != null) {
            sql.SET("release_time = #{record.releaseTime,jdbcType=VARCHAR}");
        }
        
        if (record.getRemarks() != null) {
            sql.SET("remarks = #{record.remarks,jdbcType=VARCHAR}");
        }
        
        if (record.getFirm() != null) {
            sql.SET("firm = #{record.firm,jdbcType=VARCHAR}");
        }
        
        if (record.getLatitude() != null) {
            sql.SET("latitude = #{record.latitude,jdbcType=VARCHAR}");
        }
        
        if (record.getLongitude() != null) {
            sql.SET("longitude = #{record.longitude,jdbcType=VARCHAR}");
        }
        
        if (record.getState() != null) {
            sql.SET("state = #{record.state,jdbcType=INTEGER}");
        }
        
        if (record.getCreateTime() != null) {
            sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getModifyTime() != null) {
            sql.SET("modify_time = #{record.modifyTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getInviteBidsDetail() != null) {
            sql.SET("invite_bids_detail = #{record.inviteBidsDetail,jdbcType=LONGVARCHAR}");
        }
        
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExampleWithBLOBs(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("invite_bids");
        
        sql.SET("id = #{record.id,jdbcType=INTEGER}");
        sql.SET("number_id = #{record.numberId,jdbcType=VARCHAR}");
        sql.SET("car_model = #{record.carModel,jdbcType=VARCHAR}");
        sql.SET("end_time = #{record.endTime,jdbcType=TIMESTAMP}");
        sql.SET("item_name = #{record.itemName,jdbcType=VARCHAR}");
        sql.SET("invite_id = #{record.inviteId,jdbcType=VARCHAR}");
        sql.SET("car_model_number = #{record.carModelNumber,jdbcType=VARCHAR}");
        sql.SET("car_explain = #{record.carExplain,jdbcType=VARCHAR}");
        sql.SET("reggion = #{record.reggion,jdbcType=VARCHAR}");
        sql.SET("privince = #{record.privince,jdbcType=VARCHAR}");
        sql.SET("city = #{record.city,jdbcType=VARCHAR}");
        sql.SET("bugget = #{record.bugget,jdbcType=VARCHAR}");
        sql.SET("type = #{record.type,jdbcType=VARCHAR}");
        sql.SET("invite_bids_brief = #{record.inviteBidsBrief,jdbcType=VARCHAR}");
        sql.SET("url = #{record.url,jdbcType=VARCHAR}");
        sql.SET("other = #{record.other,jdbcType=VARCHAR}");
        sql.SET("release_time = #{record.releaseTime,jdbcType=VARCHAR}");
        sql.SET("remarks = #{record.remarks,jdbcType=VARCHAR}");
        sql.SET("firm = #{record.firm,jdbcType=VARCHAR}");
        sql.SET("latitude = #{record.latitude,jdbcType=VARCHAR}");
        sql.SET("longitude = #{record.longitude,jdbcType=VARCHAR}");
        sql.SET("state = #{record.state,jdbcType=INTEGER}");
        sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
        sql.SET("modify_time = #{record.modifyTime,jdbcType=TIMESTAMP}");
        sql.SET("invite_bids_detail = #{record.inviteBidsDetail,jdbcType=LONGVARCHAR}");
        
        InviteBidsExample example = (InviteBidsExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByExample(Map<String, Object> parameter) {
        SQL sql = new SQL();
        sql.UPDATE("invite_bids");
        
        sql.SET("id = #{record.id,jdbcType=INTEGER}");
        sql.SET("number_id = #{record.numberId,jdbcType=VARCHAR}");
        sql.SET("car_model = #{record.carModel,jdbcType=VARCHAR}");
        sql.SET("end_time = #{record.endTime,jdbcType=TIMESTAMP}");
        sql.SET("item_name = #{record.itemName,jdbcType=VARCHAR}");
        sql.SET("invite_id = #{record.inviteId,jdbcType=VARCHAR}");
        sql.SET("car_model_number = #{record.carModelNumber,jdbcType=VARCHAR}");
        sql.SET("car_explain = #{record.carExplain,jdbcType=VARCHAR}");
        sql.SET("reggion = #{record.reggion,jdbcType=VARCHAR}");
        sql.SET("privince = #{record.privince,jdbcType=VARCHAR}");
        sql.SET("city = #{record.city,jdbcType=VARCHAR}");
        sql.SET("bugget = #{record.bugget,jdbcType=VARCHAR}");
        sql.SET("type = #{record.type,jdbcType=VARCHAR}");
        sql.SET("invite_bids_brief = #{record.inviteBidsBrief,jdbcType=VARCHAR}");
        sql.SET("url = #{record.url,jdbcType=VARCHAR}");
        sql.SET("other = #{record.other,jdbcType=VARCHAR}");
        sql.SET("release_time = #{record.releaseTime,jdbcType=VARCHAR}");
        sql.SET("remarks = #{record.remarks,jdbcType=VARCHAR}");
        sql.SET("firm = #{record.firm,jdbcType=VARCHAR}");
        sql.SET("latitude = #{record.latitude,jdbcType=VARCHAR}");
        sql.SET("longitude = #{record.longitude,jdbcType=VARCHAR}");
        sql.SET("state = #{record.state,jdbcType=INTEGER}");
        sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
        sql.SET("modify_time = #{record.modifyTime,jdbcType=TIMESTAMP}");
        
        InviteBidsExample example = (InviteBidsExample) parameter.get("example");
        applyWhere(sql, example, true);
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(InviteBids record) {
        SQL sql = new SQL();
        sql.UPDATE("invite_bids");
        
        if (record.getNumberId() != null) {
            sql.SET("number_id = #{numberId,jdbcType=VARCHAR}");
        }
        
        if (record.getCarModel() != null) {
            sql.SET("car_model = #{carModel,jdbcType=VARCHAR}");
        }
        
        if (record.getEndTime() != null) {
            sql.SET("end_time = #{endTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getItemName() != null) {
            sql.SET("item_name = #{itemName,jdbcType=VARCHAR}");
        }
        
        if (record.getInviteId() != null) {
            sql.SET("invite_id = #{inviteId,jdbcType=VARCHAR}");
        }
        
        if (record.getCarModelNumber() != null) {
            sql.SET("car_model_number = #{carModelNumber,jdbcType=VARCHAR}");
        }
        
        if (record.getCarExplain() != null) {
            sql.SET("car_explain = #{carExplain,jdbcType=VARCHAR}");
        }
        
        if (record.getReggion() != null) {
            sql.SET("reggion = #{reggion,jdbcType=VARCHAR}");
        }
        
        if (record.getPrivince() != null) {
            sql.SET("privince = #{privince,jdbcType=VARCHAR}");
        }
        
        if (record.getCity() != null) {
            sql.SET("city = #{city,jdbcType=VARCHAR}");
        }
        
        if (record.getBugget() != null) {
            sql.SET("bugget = #{bugget,jdbcType=VARCHAR}");
        }
        
        if (record.getType() != null) {
            sql.SET("type = #{type,jdbcType=VARCHAR}");
        }
        
        if (record.getInviteBidsBrief() != null) {
            sql.SET("invite_bids_brief = #{inviteBidsBrief,jdbcType=VARCHAR}");
        }
        
        if (record.getUrl() != null) {
            sql.SET("url = #{url,jdbcType=VARCHAR}");
        }
        
        if (record.getOther() != null) {
            sql.SET("other = #{other,jdbcType=VARCHAR}");
        }
        
        if (record.getReleaseTime() != null) {
            sql.SET("release_time = #{releaseTime,jdbcType=VARCHAR}");
        }
        
        if (record.getRemarks() != null) {
            sql.SET("remarks = #{remarks,jdbcType=VARCHAR}");
        }
        
        if (record.getFirm() != null) {
            sql.SET("firm = #{firm,jdbcType=VARCHAR}");
        }
        
        if (record.getLatitude() != null) {
            sql.SET("latitude = #{latitude,jdbcType=VARCHAR}");
        }
        
        if (record.getLongitude() != null) {
            sql.SET("longitude = #{longitude,jdbcType=VARCHAR}");
        }
        
        if (record.getState() != null) {
            sql.SET("state = #{state,jdbcType=INTEGER}");
        }
        
        if (record.getCreateTime() != null) {
            sql.SET("create_time = #{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getModifyTime() != null) {
            sql.SET("modify_time = #{modifyTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getInviteBidsDetail() != null) {
            sql.SET("invite_bids_detail = #{inviteBidsDetail,jdbcType=LONGVARCHAR}");
        }
        
        sql.WHERE("id = #{id,jdbcType=INTEGER}");
        
        return sql.toString();
    }

    protected void applyWhere(SQL sql, InviteBidsExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            sql.WHERE(sb.toString());
        }
    }
}